package cn.com.nes.site.service.crm.opportunity.impl;


import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.mybatis.agent.crm.entity.RelationOpportunity;
import cn.com.nes.mybatis.agent.crm.mapper.RelationOpportunityMapper;
import cn.com.nes.mybatis.agent.project.entity.ProjectCustomerRelationship;
import cn.com.nes.mybatis.agent.project.entity.TProject;
import cn.com.nes.site.controller.crm.opportunity.vo.RelationOpportunityPageVO;
import cn.com.nes.site.entity.vo.project.SelectBusinessOpportunityAndSaveCustomerVO;
import cn.com.nes.site.service.crm.opportunity.RelationOpportunityService;
import cn.com.nes.site.service.crm.opportunity.bo.RelationOpportunityBO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityAndContractDTO;
import cn.com.nes.site.service.project.ProjectCustomerRelationshipService;
import cn.com.nes.site.service.project.ProjectWorkOrderDataService;
import cn.com.nes.site.service.project.TProjectService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Slf4j
@Service
@Transactional
public class RelationOpportunityServiceImpl extends ServiceImpl<RelationOpportunityMapper, RelationOpportunity> implements RelationOpportunityService {

    @Resource
    ProjectWorkOrderDataService projectWorkOrderDataService;
    @Resource
    ProjectCustomerRelationshipService projectCustomerRelationshipService;

    @Resource
    TProjectService tProjectService;
    @Override
    public void relationOpportunity(RelationOpportunityBO relationOpportunityBO) throws Exception {
        if(relationOpportunityBO.getOpportunityIdList().isEmpty()){
            throw  new BusinessException("商机不能为空");
        }
        TProject tProject=tProjectService.getProjectByopportunityUuid(relationOpportunityBO.getMainBusinessId());
       //处理像鑫磊这样已经导入的历史数据，但是商机只有一个商机，不存在关联关系的数据处理
        if(ObjectUtil.isNotEmpty(tProject)) {
            List<ProjectCustomerRelationship> list = projectCustomerRelationshipService.getAllRelationship(tProject.getProjectId());
           if(!list.isEmpty()) {
               for (ProjectCustomerRelationship s : list) {
                   relationOpportunityBO.getOpportunityIdList().add(s.getBusinessId());
               }
           }
        }
        SelectBusinessOpportunityAndSaveCustomerVO vo=new SelectBusinessOpportunityAndSaveCustomerVO();
        //利用set去重
        vo.setOpportunityIdList(new ArrayList<>(new HashSet<>(relationOpportunityBO.getOpportunityIdList())));
        vo.setMasterCustomerId(relationOpportunityBO.getMasterCustomerId());
        if(ObjectUtil.isNotEmpty(tProject)){
            vo.setProjectUuid(tProject.getUuid());
        }else {
            vo.setProjectUuid(relationOpportunityBO.getProjectUuid());
        }
        vo.setMainBusinessId(relationOpportunityBO.getMainBusinessId());
        vo.setOperateUserId(relationOpportunityBO.getOperateUserId());
        vo.setOperateDate(relationOpportunityBO.getOperateDate());
        projectWorkOrderDataService.selectBusinessOpportunityAndSaveCustomer(vo);
    }

    @Override
    public Page<OpportunityAndContractDTO>  getRelationOpportunity(RelationOpportunityPageVO relationOpportunityPageVO) {
        Page<OpportunityAndContractDTO> pageList =new Page<>(relationOpportunityPageVO.getPageid(), relationOpportunityPageVO.getPagesize());
        List<OpportunityAndContractDTO> list= this.baseMapper.getRelationOpportunity(pageList,relationOpportunityPageVO.getMainBusinessId(),relationOpportunityPageVO.getOpportunityName(),relationOpportunityPageVO.getCustomerName());
        return pageList.setRecords(list);
    }

    @Override
    public void delRelationOpportunity(RelationOpportunityBO relationOpportunityBO) throws Exception {
        projectCustomerRelationshipService.deleteOpportunityId(relationOpportunityBO.getOpportunityIdList(), relationOpportunityBO.getMainBusinessId(),relationOpportunityBO.getOperateUserId(), relationOpportunityBO.getOperateDate());
    }
}
